//
// Created by codex on 2024/5/29.
//

#include "Peripheral.h"
#include "usart.h"


PERI PERI_1;
union FloatData temp;
unsigned char camera_x=0,camera_y=0,mode=0,color_select=0;
uint8_t up_send_buffer[256];
void Mix_Send_Data_2Up(uint8_t frame_num)
{
    //u8 cnt = 2;

    up_send_buffer[0] = 0xAA;
    up_send_buffer[1] = 0xFF;
    switch (frame_num) {
        case 0xE0: {  //上报v831识别数据
            up_send_buffer[2] = 0xE0;
//            send_buffer[cnt++] = 10;
            up_send_buffer[3] = 0x04;
            up_send_buffer[4] = mode;
            up_send_buffer[5] = color_select;
            up_send_buffer[6] = camera_x;
            up_send_buffer[7] = camera_y;
            break;
        }
        case 0xF4: //上报v831模式数据
        {
            up_send_buffer[2] = 0xF4;
            up_send_buffer[3] = 0x01;
            up_send_buffer[4] = PERI_1.CAMERA_MODE;
            break;
        }
        case IR_TYPE: //上报红外测温数据
        {
            up_send_buffer[2] = 0xE1;
            up_send_buffer[3] = 0x04;
            up_send_buffer[4] = temp.u8Value[0];
            up_send_buffer[5] = temp.u8Value[1];
            up_send_buffer[6] = temp.u8Value[2];
            up_send_buffer[7] = temp.u8Value[3];
            break;
        }
        case PER_STATE_TYPE:
        {
            up_send_buffer[2] = PER_STATE_TYPE;
            up_send_buffer[3] = 5;
            up_send_buffer[4]=  (PERI_1.LED_RED_STA-0xA0) << 3 |
                                (PERI_1.LED_GREEN_STA-0xA0)<<2 |
                                (PERI_1.LED_BLUE_STA-0xA0) <<1 |
                                (PERI_1.LED_WHITE_STA-0xA0);
            up_send_buffer[5]=PERI_1.CAMERA_MODE;
            up_send_buffer[6]=PERI_1.MOTOR_1_STA;
            up_send_buffer[7]=PERI_1.BEEP_STA;
            up_send_buffer[8]=PERI_1.LASER_STA;
            break;
        }
        default:
            break;
    }

    uint8_t check_sum1 = 0, check_sum2 = 0;
    for (uint8_t i = 0; i < up_send_buffer[3] + 4; i++) {
        check_sum1 += up_send_buffer[i];
        check_sum2 += check_sum1;
    }
    up_send_buffer[up_send_buffer[3] + 4] = check_sum1;
    up_send_buffer[up_send_buffer[3] + 5] = check_sum2;
    //print_UARTx(&huart3,up_send_buffer);
    for (int j = 0; j < up_send_buffer[3] + 6; j++)
    {
        HAL_UART_Transmit(&huart3, (uint8_t *)up_send_buffer+j, 1, 100);
    }
}
uint8_t camera_send_buffer[10];
void Mix_Send_Data_2Camera(uint8_t frame_num) //切换v831模式
{
    camera_send_buffer[0] = 0x3f;
    camera_send_buffer[2] = 0xFF;
    switch (frame_num) {
        case 0xF4:
        {
            camera_send_buffer[1] = PERI_1.CAMERA_MODE;
        }
        default:
            break;
    }
    //print_UARTx(&huart3,camera_send_buffer);//测试用
    print_UARTx(&huart4,camera_send_buffer);//实际用
}

void UpperComputer_DataAnl(uint8_t *data_t)
{
    uint8_t len=strlen(data_t);
    uint8_t len_t=len;
    uint8_t data[128];
    memset(data,0,sizeof(data));
    memcpy(data,data_t,len);
//    PERI_1.BEEP_STA=0xA1;
//    PERI_1.BEEP_FLAG=0xA1;

    while(len_t>0)
    {
        //print_UARTx(&huart3,"len:%d\r\n",len_t);
        //print_UARTx(&huart3,data);
        len=data[3]+6;
        uint8_t check_sum1 = 0, check_sum2 = 0;
        for (uint8_t i = 0; i < len - 2; i++)
        {
            check_sum1 += *(data + i);
            check_sum2 += check_sum1;
        }
        if ((check_sum1 != *(data + len - 2)) || (check_sum2 != *(data + len - 1))) //ÅÐ¶ÏsumÐ£Ñé
            return;
        switch (*(data + 2))
        {
            case BEEP_TYPE:
            {
                PERI_1.BEEP_STA=*(data+4);
                //PERI_1.BEEP_STA=0xA1;
                PERI_1.BEEP_FLAG=0xA1;
                break;
            }
            case LASER_TYPE:
            {
                PERI_1.LASER_STA=*(data+4);
                break;
            }
            case MOTOR_TYPE:
            {
                switch (*(data+4))
                {
                    case RELEASE_ZERO:
                    {
                        PERI_1.MOTOR_1_STA=RELEASE_ZERO;
                        break;
                    }
                    case RELEASE_ONE:
                    {
                        PERI_1.MOTOR_1_STA=RELEASE_ONE;
                        break;
                    }
                    case RELEASE_TWO:
                    {
                        PERI_1.MOTOR_1_STA=RELEASE_TWO;
                        break;
                    }
                    case RELEASE_THREE:
                    {
                        PERI_1.MOTOR_1_STA=RELEASE_THREE;
                        break;
                    }

                }
                break;
            }
            case CAMERA_TYPE:
            {
                switch(*(data+4))
                {
                    case TURN_RED:
                    {
                        PERI_1.LED_WHITE_STA=0xA0;
                        PERI_1.LED_BLUE_STA=0xA0;
                        PERI_1.LED_GREEN_STA=0xA0;
                        PERI_1.LED_RED_STA=0xA0;
                        PERI_1.CAMERA_MODE=1;
                        PERI_1.LED_RED_STA=0xA1;
                        break;
                    }
                    case TURN_GREEN:
                    {
                        PERI_1.LED_WHITE_STA=0xA0;
                        PERI_1.LED_BLUE_STA=0xA0;
                        PERI_1.LED_GREEN_STA=0xA0;
                        PERI_1.LED_RED_STA=0xA0;
                        PERI_1.CAMERA_MODE=2;
                        PERI_1.LED_GREEN_STA=0xA1;
                        break;
                    }
                    case TURN_BLUE:
                    {
                        PERI_1.LED_WHITE_STA=0xA0;
                        PERI_1.LED_BLUE_STA=0xA0;
                        PERI_1.LED_GREEN_STA=0xA0;
                        PERI_1.LED_RED_STA=0xA0;
                        PERI_1.CAMERA_MODE=3;
                        PERI_1.LED_BLUE_STA=0xA1;
                        break;
                    }
                    case TURN_BLACK:
                    {
                        PERI_1.LED_WHITE_STA=0xA0;
                        PERI_1.LED_BLUE_STA=0xA0;
                        PERI_1.LED_GREEN_STA=0xA0;
                        PERI_1.LED_RED_STA=0xA0;
                        PERI_1.CAMERA_MODE=4;
                        PERI_1.LED_WHITE_STA=0xA1;
                        break;
                    }
                }
                //Mix_Send_Data_2Camera(0xF4);
                //PERI_1.CAMERA_MODE=0;
                break;
            }
            case LED_TYPE:
            {
//                PERI_1.LED_WHITE_STA=0xA0;
//                PERI_1.LED_BLUE_STA=0xA0;
//                PERI_1.LED_GREEN_STA=0xA0;
//                PERI_1.LED_RED_STA=0xA0;
                switch (*(data+4))
                {
                    case TURN_WHITE:
                    {
                        PERI_1.LED_WHITE_STA=0xA1;
                        break;
                    }
                    case TURN_BLUE:
                    {
                        PERI_1.LED_BLUE_STA=0xA1;
                        break;
                    }
                    case TURN_GREEN:
                    {
                        PERI_1.LED_GREEN_STA=0xA1;
                        break;
                    }
                    case TURN_RED:
                    {
                        PERI_1.LED_RED_STA=0xA1;
                        break;
                    }
                    case OFF:
                    {
                        PERI_1.LED_WHITE_STA=0xA0;
                        PERI_1.LED_BLUE_STA=0xA0;
                        PERI_1.LED_GREEN_STA=0xA0;
                        PERI_1.LED_RED_STA=0xA0;
                        break;
                    }
                }
                break;
            }
            case SERVO_TYPE:
            {
                switch (*(data+4))
                {
                    case ON:
                    {
                        PERI_1.SERVO_STA=0xA1;
                        break;
                    }
                    case OFF:
                    {
                        PERI_1.SERVO_STA=0xA0;
                        break;
                    }
                }
            }
        }

        len_t-=data[3]+6;
        memcpy(data,data+data[3]+6,strlen(data)-data[3]-6);
    }
    //UpperComputer_send_data("get data1");
//    if (*(data + 3) != (len - 6)) //ÅÐ¶ÏÊý¾Ý³¤¶ÈÊÇ·ñÕýÈ·
//        return;
}

void myPERI_init()
{
    PERI_1.BEEP_STA=0xA0;
    PERI_1.BEEP_FLAG=0xA0;
    PERI_1.LASER_STA=0xA0;
    PERI_1.LED_BLUE_STA=0xA0;
    PERI_1.LED_GREEN_STA=0xA0;
    PERI_1.LED_RED_STA=0xA0;
    PERI_1.LED_WHITE_STA=0xA0;
    PERI_1.MOTOR_1_STA=RELEASE_ZERO;
    PERI_1.SERVO_STA=0xA0;
}
//void Peripheral_Task()
//{
//
//}